home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Burning & Media / GB-PVR 1.2.13 / GBPVR10213.msi / Cabs.w1.cab / Info.aspx.cs649 < prev    next >
Text File  |  2008-04-01  |  15KB  |  432 lines

  1. //===========================================================================
  2. // This file was modified as part of an ASP.NET 2.0 Web project conversion.
  3. // The class name was changed and the class modified to inherit from the abstract base class 
  4. // in file 'App_Code\Migrated\Stub_Info_aspx_cs.cs'.
  5. // During runtime, this allows other classes in your web application to bind and access 
  6. // the code-behind page using the abstract base class.
  7. // The associated content page 'Info.aspx' was also modified to refer to the new class name.
  8. // For more information on this code pattern, please refer to http://go.microsoft.com/fwlink/?LinkId=46995 
  9. //===========================================================================
  10. using System;
  11. using System.Collections;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Data.Common;
  15. using System.Data.OleDb;
  16. using System.Drawing;
  17. using System.Text.RegularExpressions;
  18. using System.IO;
  19. using System.Management;
  20. using System.Runtime.InteropServices;
  21. using System.Runtime.Remoting.Channels;
  22. using System.Runtime.Remoting.Channels.Tcp;
  23. using System.Web;
  24. using System.Web.Caching;
  25. using System.Web.Security;
  26. using System.Web.SessionState;
  27. using System.Web.UI;
  28. using System.Web.UI.WebControls;
  29. using System.Web.UI.HtmlControls;
  30. using System.Xml;
  31.  
  32. using GBPVR.Public;
  33. using GBPVR.Backend.Common;
  34. using GBPVRSchedule;
  35. using gbweb.classes;
  36. // For IRecordingService
  37.  
  38. namespace gbweb
  39. {
  40.     /// <summary>
  41.     /// Summary description for Info.
  42.     /// </summary>
  43.     public partial class Migrated_Info : Info
  44.     {
  45.  
  46.         protected void Page_Load(object sender, System.EventArgs e)
  47.         {
  48.  
  49.             Session["GuideStartTime"] = null;
  50.             GetDiskInformation("RecordingsDirectory", lblRecordingStats, imgRecordingUsedSystem, imgRecordingUsedGBPVR, imgRecordingFree);
  51.             GetDiskInformation("LiveTVDirectory", lblBufferStats, imgBufferUsedSystem, imgBufferUsedGBPVR, imgBufferFree);
  52.             hlLogFile.Text = AppDomain.CurrentDomain.FriendlyName + ".log";
  53.             hlLogFile.NavigateUrl = Download.GetDownloadUrl(false, true, Download.InternalFiles.Log);
  54.  
  55.             Schedule scheduleHelper = Global.Schedule;
  56.             IList myScheduledRecordings = scheduleHelper.LoadRecordingSchedule();
  57.             lblScheduleStatus.Text =
  58.                 GetScheduleStatus(myScheduledRecordings, "Pending", ScheduledRecording.STATUS_PENDING) + ", " +
  59.                 GetScheduleStatus(myScheduledRecordings, "In Progress", ScheduledRecording.STATUS_IN_PROGRESS) + ", " +
  60.                 GetScheduleStatus(myScheduledRecordings, "Available", ScheduledRecording.STATUS_COMPLETED) + ", " +
  61.                 GetScheduleStatus(myScheduledRecordings, "Failed", ScheduledRecording.STATUS_COMPLETED_WITH_ERROR) + ", " +
  62.                 GetScheduleStatus(myScheduledRecordings, "Conflict", ScheduledRecording.STATUS_CONFLICT) + ", " +
  63.                 GetScheduleStatus(myScheduledRecordings, "Reoccurring", ScheduledRecording.STATUS_PLACE_HOLDER) + ", " +
  64.                 GetScheduleStatus(myScheduledRecordings, "Deleted", ScheduledRecording.STATUS_DELETED);
  65.  
  66.             try
  67.             {
  68.                 GetRecordingStatuses();
  69.             }
  70.             catch (Exception ex)
  71.             {
  72.                 Logger.Error(ex.ToString());
  73.                 Response.Write("<!--\n" + ex.ToString() + "\n-->");
  74.             }
  75.  
  76.             // create the database connection
  77.             using (DbConnection aConnection = Global.GetOpenGBPVRDbConnection())
  78.             {
  79.                 try
  80.                 {
  81.                     lblEPGRemaining.Text = GetEPGRemaining(aConnection);
  82.                 }
  83.                 catch (Exception ex)
  84.                 {
  85.                     Logger.Error(ex.ToString());
  86.                     Response.Write("<!--\n" + ex.ToString() + "\n-->");
  87.                 }
  88.  
  89.                 try
  90.                 {
  91.                     lblCurrentTranscode.Text = GetCurrentTranscode(aConnection);
  92.                 }
  93.                 catch (Exception ex)
  94.                 {
  95.                     Logger.Error(ex.ToString());
  96.                     Response.Write("<!--\n" + ex.ToString() + "\n-->");
  97.                 }
  98.  
  99.                 //close the connection
  100.                 aConnection.Close();
  101.             }
  102.  
  103.         }
  104.  
  105.         #region Disk Information
  106.         private void GetDiskInformation(string nodeName, Label lblStatus, HtmlImage imgUsedSystem, HtmlImage imgUsedGBPVR, HtmlImage imgFree)
  107.         {
  108.             string pathName = Global.Config.SelectSingleNode("/settings/" + nodeName).InnerText;
  109.  
  110.             string scope;
  111.             string root;
  112.             ulong FreeBytesAvailable = 0;
  113.             ulong TotalNumberOfBytes = 0;
  114.             ulong TotalNumberOfFreeBytes = 0;
  115.             bool nonSharedMap = false;
  116.  
  117.             double size = 0;
  118.             double free = 0;
  119.  
  120.             if (pathName.StartsWith(@"\\"))
  121.             {
  122.                 scope = pathName.Substring(0, pathName.IndexOf(@"\", 2));
  123.                 root = Directory.GetDirectoryRoot(pathName);
  124.                 string shareName = root.Substring(scope.Length + 1);
  125.  
  126.                 bool success = GetDiskFreeSpaceEx(root, out FreeBytesAvailable, out TotalNumberOfBytes,
  127.                                    out TotalNumberOfFreeBytes);
  128.                 if (!success)
  129.                 {
  130.                     Logger.Info("Falling through to wmi for free space determination.....");
  131.                     //throw new System.ComponentModel.Win32Exception();
  132.                     ManagementObject share = new ManagementObject(scope + @"\root\cimv2", "Win32_Share.Name=\"" + shareName + "\"", null);
  133.                     try
  134.                     {
  135.                         share.Get();
  136.                     }
  137.                     catch (Exception ex)
  138.                     {
  139.                         lblStatus.Text = "Could not load share info for " + root;
  140.                         imgFree.Visible = false;
  141.                         imgUsedGBPVR.Visible = false;
  142.                         imgUsedSystem.Visible = false;
  143.                         Logger.Warning(ex.ToString());
  144.                         share.Dispose();
  145.                         return;
  146.                     }
  147.  
  148.                     try
  149.                     {
  150.                         root = share["Path"].ToString();
  151.                     }
  152.                     catch (Exception ex)
  153.                     {
  154.                         lblStatus.Text = "Could not load share info for " + root;
  155.                         imgFree.Visible = false;
  156.                         imgUsedGBPVR.Visible = false;
  157.                         imgUsedSystem.Visible = false;
  158.                         Logger.Warning(ex.ToString());
  159.                         share.Dispose();
  160.                         return;
  161.                     }
  162.                     share.Dispose();
  163.                 }
  164.                 else
  165.                 {
  166.                     Logger.Info("Using native win32 call for free space determination.....");
  167.                     nonSharedMap = true;
  168.                 }
  169.             }
  170.             else
  171.             {
  172.                 Logger.Info("Using wmi for free space determination.....");
  173.                 scope = @"\\.";
  174.                 root = Directory.GetDirectoryRoot(pathName);
  175.             }
  176.  
  177.             if (!nonSharedMap)
  178.             {
  179.                 root = root.Replace(Path.DirectorySeparatorChar.ToString(), string.Empty);
  180.  
  181.                 ManagementObject disk = new ManagementObject(scope + @"\root\cimv2", "Win32_LogicalDisk.DeviceID=\"" + root + "\"", null);
  182.  
  183.                 try
  184.                 {
  185.                     disk.Get();
  186.                 }
  187.                 catch (Exception ex)
  188.                 {
  189.                     lblStatus.Text = "Could not load disk info for " + Directory.GetDirectoryRoot(pathName);
  190.                     imgFree.Visible = false;
  191.                     imgUsedGBPVR.Visible = false;
  192.                     imgUsedSystem.Visible = false;
  193.                     Logger.Warning(ex.ToString());
  194.                     disk.Dispose();
  195.                     return;
  196.                 }
  197.                 size = Convert.ToInt64(disk["Size"]);
  198.                 free = Convert.ToInt64(disk["FreeSpace"]);
  199.                 disk.Dispose();
  200.             }
  201.             else
  202.             {
  203.                 size = Convert.ToInt64(TotalNumberOfBytes);
  204.                 free = Convert.ToInt64(FreeBytesAvailable);
  205.             }
  206.             double usedgbpvr = GetDirectorySize(pathName);
  207.             double usedsystem = size - free - usedgbpvr;
  208.  
  209.             double freepercent = ((double)free/(double)size) * 100.0;
  210.             double usedgbpvrpercent = ((double)usedgbpvr/(double)size) * 100.0;
  211.             double usedsystempercent = ((double)usedsystem/(double)size) * 100.0;
  212.  
  213.             string sizeunit = "B";
  214.             string freeunit = "B";
  215.             string usedgbpvrunit = "B";
  216.             string usedsystemunit = "B";
  217.  
  218.             ReduceSIUnit(ref size, ref sizeunit);
  219.             ReduceSIUnit(ref free, ref freeunit);
  220.             ReduceSIUnit(ref usedgbpvr, ref usedgbpvrunit);
  221.             ReduceSIUnit(ref usedsystem, ref usedsystemunit);
  222.  
  223.             lblStatus.Text = Directory.GetDirectoryRoot(pathName) + " " +
  224.                 size.ToString("0.0") + " " + sizeunit + " Total, " +
  225.                 free.ToString("0.0") + " " + freeunit + " Free, " +
  226.                 freepercent.ToString("#0.00") + "% Free";
  227.  
  228.             double barlength = 300;
  229.             imgFree.Width = (int)(freepercent * barlength / 100);
  230.             imgFree.Visible = imgFree.Width > 0;
  231.             imgUsedGBPVR.Width = (int)(usedgbpvrpercent * barlength / 100);
  232.             imgUsedGBPVR.Visible = imgUsedGBPVR.Width > 0;
  233.             imgUsedSystem.Width = (int)(usedsystempercent * barlength / 100);
  234.             imgUsedSystem.Visible = imgUsedSystem.Width > 0;
  235.  
  236.             imgUsedSystem.Attributes["title"] =
  237.                 usedsystem.ToString("0.0") + " " + usedsystemunit + " Used (System), " +
  238.                 usedsystempercent.ToString("#0.00") + "%";
  239.             imgUsedGBPVR.Attributes["title"] =
  240.                 usedgbpvr.ToString("0.0") + " " + usedgbpvrunit + " Used (GB-PVR), " +
  241.                 usedgbpvrpercent.ToString("#0.00") + "%";
  242.             imgFree.Attributes["title"] =
  243.                 free.ToString("0.0") + " " + freeunit + " Free, " +
  244.                 freepercent.ToString("#0.00") + "%";
  245.         }
  246.  
  247.         [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  248.         [return: MarshalAs(UnmanagedType.Bool)]
  249.         static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
  250.         out ulong lpFreeBytesAvailable,
  251.         out ulong lpTotalNumberOfBytes,
  252.         out ulong lpTotalNumberOfFreeBytes);
  253.  
  254.         private void ReduceSIUnit(ref double value, ref string unit)
  255.         {
  256.             int unitpos = 0;
  257.             string[] units = new string[] {"", "K", "M", "G", "T", "P"};
  258.             while (value > 1024) { value = value / 1024; unitpos++; }
  259.             unit = units[unitpos] + unit;
  260.         }
  261.  
  262.         private long GetDirectorySize(string pathName)
  263.         {
  264.             long totalsize = 0;
  265.             FileSystemInfo[] infos = (new DirectoryInfo(pathName)).GetFileSystemInfos();
  266.             foreach (FileSystemInfo info in infos)
  267.             {
  268.                 long itemsize = 0;
  269.                 try
  270.                 {
  271.                     if (info is FileInfo)
  272.                     {
  273.                         itemsize = (info as FileInfo).Length;
  274.                     }
  275.                     else if (info is DirectoryInfo)
  276.                     {
  277.                         itemsize = GetDirectorySize((info as DirectoryInfo).FullName);
  278.                     }
  279.                 }
  280.                 catch (Exception ex)
  281.                 {
  282.                     Logger.Error(ex.ToString());
  283.                     Response.Write("<!--\n" + ex.ToString() + "\n-->");
  284.                 }
  285.                 totalsize += itemsize;
  286.             }
  287.             return totalsize;
  288.         }
  289.         #endregion
  290.  
  291.         #region Schedule Status
  292.         private string GetScheduleStatus(IList myScheduledRecordings, string name, int status)
  293.         {
  294.             int count = 0;
  295.             foreach (ScheduledRecording scheduledRecording in myScheduledRecordings)
  296.             {
  297.                 if (scheduledRecording.getRecordingStatus() == status) count++;
  298.             }
  299.             return count + " " + name;
  300.         }
  301.         #endregion
  302.  
  303.         #region Recording Status
  304.         private void GetRecordingStatuses()
  305.         {
  306.             string[] statuses = GetRecordingService().getTunerStatus();
  307.             rptRecordingSource.DataSource = statuses;
  308.             rptRecordingSource.DataBind();
  309.         }
  310.  
  311.         protected string GetSourceName(RepeaterItem Container)
  312.         {
  313.             string value = Container.DataItem.ToString();
  314.             value = value.Substring(0, value.IndexOf(":"));
  315.             if (value == "My Source Name")
  316.                 return "Capture Source #" + (Container.ItemIndex + 1);
  317.             return value;
  318.         }
  319.  
  320.         protected string GetSourceStatus(RepeaterItem Container)
  321.         {
  322.             string value = Container.DataItem.ToString();
  323.             value = value.Substring(value.IndexOf(":") + 1);
  324.             if (liveTVRegex.IsMatch(Container.DataItem.ToString()))
  325.             {
  326.                 value =
  327.                     "<a href=\"" + Download.GetDownloadUrl(false, false, Download.InternalFiles.LiveTV, Container.ItemIndex.ToString()) + "\" >" + value + "</a>";
  328.             }
  329.             return value;
  330.         }
  331.         #endregion
  332.  
  333.         #region EPG Remaining
  334.  
  335.         string GetEPGRemaining(DbConnection aConnection)
  336.         {
  337.             // create the command object and store the sql query
  338.             string SQLstr = "SELECT MAX(start_time) FROM PROGRAMME";
  339.             DbCommand aCommand = aConnection.CreateCommand();
  340.             aCommand.CommandText = SQLstr;
  341.             aCommand.Connection = aConnection;
  342.  
  343.             //create the datareader object to connect to table
  344.             DbDataReader aReader = aCommand.ExecuteReader();
  345.  
  346.             DateTime maxDate = DateTime.MinValue;
  347.             while(aReader.Read())
  348.             {
  349.                 maxDate = aReader.GetDateTime(0);
  350.             }
  351.             //close the reader
  352.             aReader.Close();
  353.  
  354.             if (maxDate != DateTime.MinValue)
  355.             {
  356.                 TimeSpan span = maxDate.Subtract(DateTime.Today);
  357.                 return "You have guide data until " + maxDate.ToShortDateString() +
  358.                     " (" + span.TotalDays.ToString("0") + " days)";
  359.             }
  360.             else
  361.             {
  362.                 throw new DataException();
  363.             }
  364.         }
  365.  
  366.         #endregion
  367.  
  368.         #region Current Transcode
  369.  
  370.         string GetCurrentTranscode(DbConnection aConnection)
  371.         {
  372.             // create the command object and store the sql query
  373.             string SQLstr = "SELECT t.percentage, r.filename FROM TRANSCODE_QUEUE t, " +
  374.                 "RECORDING_SCHEDULE r WHERE (r.oid = t.scheduled_recording_oid) " +
  375.                 "AND (t.status = 1)";
  376.             DbCommand aCommand = aConnection.CreateCommand();
  377.             aCommand.CommandText = SQLstr;
  378.             aCommand.Connection = aConnection;
  379.  
  380.             //create the datareader object to connect to table
  381.             DbDataReader aReader = aCommand.ExecuteReader();
  382.  
  383.             string labelText = string.Empty;
  384.             while(aReader.Read())
  385.             {
  386.                 int percentage = aReader.GetInt32(0);
  387.                 string filename = aReader.GetString(1);
  388.  
  389.                 if (percentage > 100)
  390.                     percentage = 100;
  391.  
  392.                 filename = Path.GetFileNameWithoutExtension(filename);
  393.  
  394.                 if (labelText.Length > 0)
  395.                     labelText = labelText + "<br/>";
  396.  
  397.                 labelText = labelText + filename + " : " + percentage + "% Complete";
  398.             }
  399.  
  400.             //close the reader
  401.             aReader.Close();
  402.  
  403.             if (labelText.Length > 0)
  404.                 return labelText;
  405.             return "There are currently no files being transcoded";
  406.         }
  407.  
  408.         #endregion
  409.  
  410.         #region Web Form Designer generated code
  411.         override protected void OnInit(EventArgs e)
  412.         {
  413.             //
  414.             // CODEGEN: This call is required by the ASP.NET Web Form Designer.
  415.             //
  416.             InitializeComponent();
  417.             base.OnInit(e);
  418.         }
  419.  
  420.         /// <summary>
  421.         /// Required method for Designer support - do not modify
  422.         /// the contents of this method with the code editor.
  423.         /// </summary>
  424.         private void InitializeComponent()
  425.         {
  426.  
  427.         }
  428.         #endregion
  429.  
  430.     }
  431. }
  432.